package com.demo.search.spring;

import org.apache.lucene.index.IndexWriter;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.demo.search.context.IndexContext;
import com.demo.search.context.impl.LocalIndexContext;
import com.demo.search.engine.SearchEngine;

/**
 * 索引容器上下文工厂类
 * @author ryuu.kk
 */
public class LocalIndexContextFactoryBean implements FactoryBean<IndexContext>, InitializingBean {

	/**
	 * 索引容器上下文
	 */
	private IndexContext context;
	
	/**
	 * 写入索引类
	 */
	private IndexWriter indexWriter;
	
	/**
	 * 对象-文档模型转化引擎
	 */
	private SearchEngine engine;
	
	@Override
	public IndexContext getObject() throws Exception {
		context = new LocalIndexContext(engine);
		return this.context;
	}

	@Override
	public Class<?> getObjectType() {
		return com.demo.search.context.IndexContext.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		indexWriter = engine.ownerIndexWriter();
		Assert.notNull(indexWriter);
		Assert.notNull(engine);
		
	}

	public void setEngine(SearchEngine engine) {
		this.engine = engine;
	}
}
